/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaEntrada.java
 *
 * Created on 06/12/2012, 19:23:36
 */
package gui;

import Fachada.Fachada;
import classes.Entrada;
import classes.Fornecedor;
import classes.ItensProduto;
import classes.Produto;
import classes.teclasPermitidas;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.DaoException;
import erro.GeralException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author George
 */
public class telaEntrada extends javax.swing.JFrame {

    private static Fachada f;
    String cnpj;

    /**
     * Creates new form telaEntrada
     */
    public telaEntrada() {
        initComponents();
        this.setLocationRelativeTo(null);
        LabelDataEntrada.setText(getDateTime());
        txtBoxQuantProd.setDocument(new teclasPermitidas());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtBoxProd = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtBoxNumLote = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtBoxQuantProd = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButtonSalvarEnt = new javax.swing.JButton();
        jButtonCancelEnt = new javax.swing.JButton();
        jButtonPesqFornec = new javax.swing.JButton();
        jButtonPesqProd = new javax.swing.JButton();
        txtBoxCNPJFornecEnt = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        txtBoxDataValid = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaObsEnt = new javax.swing.JTextPane();
        jLabel8 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        LabelDataEntrada = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Data Entrada:");

        jLabel2.setText("Fornecedor:");

        jLabel3.setText("Produto:");

        jLabel4.setText("Número do Lote:");

        jLabel5.setText("Quant. Itens:");

        jLabel6.setText("Observações:");

        jButtonSalvarEnt.setText("SALVAR");
        jButtonSalvarEnt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarEntActionPerformed(evt);
            }
        });

        jButtonCancelEnt.setText("CANCELAR");
        jButtonCancelEnt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelEntActionPerformed(evt);
            }
        });

        jButtonPesqFornec.setText("PESQUISAR");
        jButtonPesqFornec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesqFornecActionPerformed(evt);
            }
        });

        jButtonPesqProd.setText("PESQUISAR");
        jButtonPesqProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesqProdActionPerformed(evt);
            }
        });

        try {
            txtBoxCNPJFornecEnt.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel7.setText("Data Validade:");

        try {
            txtBoxDataValid.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jScrollPane1.setViewportView(txtAreaObsEnt);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel8.setText("Entrada de Produto");

        LabelDataEntrada.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(132, 132, 132)
                        .addComponent(jLabel8))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(166, 166, 166)
                                .addComponent(jButtonSalvarEnt, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(jButtonCancelEnt, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel6)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel5))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(LabelDataEntrada)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtBoxQuantProd, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtBoxCNPJFornecEnt, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jButtonPesqFornec))
                                            .addComponent(txtBoxProd, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtBoxNumLote, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonPesqProd))
                                    .addComponent(txtBoxDataValid, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(LabelDataEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBoxCNPJFornecEnt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonPesqFornec))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtBoxProd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonPesqProd))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtBoxNumLote, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtBoxQuantProd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtBoxDataValid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(8, 8, 8))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonCancelEnt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonSalvarEnt, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(35, 35, 35))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonPesqFornecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesqFornecActionPerformed
        telaPesquisarFornecedor tlpesqfornec = new telaPesquisarFornecedor();
        tlpesqfornec.setVisible(true);
        /*
         Fornecedor fornec = new Fornecedor();
         f = Fachada.getInstancia();
         cnpj = txtBoxCNPJFornecEnt.getText().replace(".", "");
         cnpj = cnpj.replace("-", "");
         cnpj = cnpj.replace("/", "");
         fornec.setCnpjFornecedor(cnpj);
         try {
         f.pesquisarFornec(fornec);
         if (fornec.getNomeFornecedor() == null) {
         telaPesquisarFornecedor tlpesqfornec = new telaPesquisarFornecedor();
         tlpesqfornec.setVisible(true);
         } else {
         }
         } catch (DaoException e) {
         JOptionPane.showMessageDialog(null, e.getMessage());
         } catch (GeralException ex) {
         JOptionPane.showMessageDialog(null, ex.getMessage());
         }
         */
    }//GEN-LAST:event_jButtonPesqFornecActionPerformed

    private void jButtonPesqProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesqProdActionPerformed
        telaPesquisarProduto tlpesqprod = new telaPesquisarProduto();
        tlpesqprod.setVisible(true);
        /*
        Produto prod = new Produto();
        f = Fachada.getInstancia();
        prod.setDescProduto(txtBoxProd.getText());
        try {
            f.pesquisarProd(prod);
            if (prod.getCodProduto() == null) {
                telaPesquisarProduto tlpesqprod = new telaPesquisarProduto();
                tlpesqprod.setVisible(true);
            } else {
            }
        } catch (DaoException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        } catch (GeralException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        * */
    }//GEN-LAST:event_jButtonPesqProdActionPerformed

    private void jButtonSalvarEntActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarEntActionPerformed
        f = Fachada.getInstancia();
        Fornecedor fornec = new Fornecedor();
        Produto prod = new Produto();
        cnpj = txtBoxCNPJFornecEnt.getText().replace(".", "");
        cnpj = cnpj.replace("-", "");
        cnpj = cnpj.replace("/", "");
        fornec.setCnpjFornecedor(cnpj);
        prod.setDescProduto(txtBoxProd.getText());
        try {
            f.pesquisarFornec(fornec);
            f.pesquisarProd(prod);
        } catch (DaoException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        } catch (GeralException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }

        Entrada ent = new Entrada();
        ItensProduto itensprod = new ItensProduto();

        ent.setDataEntrada(LabelDataEntrada.getText());
        ent.setCodFornecedor(fornec);
        ent.setCodProduto(prod);
        ent.setNumLote(txtBoxNumLote.getText());
        ent.setQuantprod(Integer.parseInt(txtBoxQuantProd.getText()));
        ent.setObsEntrada(txtAreaObsEnt.getText());

        itensprod.setCodProduto(prod);
        itensprod.setNumLote(ent.getNumLote());
        itensprod.setQuantItens(ent.getQuantprod());
        itensprod.setDataValidade(txtBoxDataValid.getText());

        prod.setCodProduto(prod.getCodProduto());
        try {
            f.inserirEnt(ent, itensprod, prod);
            limpar();
        } catch (DaoException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        } catch (ConsultaException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ConexaoException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (GeralException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

    }//GEN-LAST:event_jButtonSalvarEntActionPerformed

    private void jButtonCancelEntActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelEntActionPerformed
        telaEntrada tlent = new telaEntrada();
        int i = JOptionPane.showConfirmDialog(null, "Deseja realmente Cancelar?", "Cancelar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaEntrada.this.setVisible(false);
            tlent.dispose();
        } else {
            repaint();
        }
    }//GEN-LAST:event_jButtonCancelEntActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaEntrada.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new telaEntrada().setVisible(true);
            }
        });
    }

    public void limpar() {
        txtBoxCNPJFornecEnt.setValue("");
        txtBoxProd.setText("");
        txtBoxNumLote.setText("");
        txtBoxQuantProd.setText("");
        txtBoxDataValid.setText("");
        txtAreaObsEnt.setText("");
    }

    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        return dateFormat.format(date);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelDataEntrada;
    private javax.swing.JButton jButtonCancelEnt;
    private javax.swing.JButton jButtonPesqFornec;
    private javax.swing.JButton jButtonPesqProd;
    private javax.swing.JButton jButtonSalvarEnt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextPane txtAreaObsEnt;
    private javax.swing.JFormattedTextField txtBoxCNPJFornecEnt;
    private javax.swing.JFormattedTextField txtBoxDataValid;
    private javax.swing.JTextField txtBoxNumLote;
    public javax.swing.JTextField txtBoxProd;
    private javax.swing.JTextField txtBoxQuantProd;
    // End of variables declaration//GEN-END:variables
}
